package com.example.za.account.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.za.account.R;
import com.example.za.account.model.User;
import com.example.za.account.utils.L;
import com.example.za.account.utils.MD5Encrypt;
import com.example.za.account.utils.StringUtils;
import com.example.za.account.utils.T;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_reg_username)
    EditText mRegUsername;
    @BindView(R.id.et_reg_email)
    EditText mRegEmail;
    @BindView(R.id.et_reg_pass)
    EditText mRegPass;
    @BindView(R.id.btn_register)
    Button mBtnRegistr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitle(R.string.text_register);
        showBackwardView(true);


    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }

    @OnClick(R.id.btn_register)
    //注册按钮，执行的操作
    public void registerClick() {
        //获取输入框中用户输入的信息
        final String name = mRegUsername.getText().toString().trim();
        final String pass = mRegPass.getText().toString().trim();
        final String email = mRegEmail.getText().toString().trim();
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(email)) {
            //检测邮箱号是否符合格式
            if (StringUtils.checkEmail(email)) {
                //开启异步线程，避免阻塞UI主线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        BmobQuery<User> query = new BmobQuery<>();
                        query.addWhereEqualTo("email", email);
                        query.findObjects(new FindListener<User>() {

                            @Override
                            public void done(List<User> list, BmobException e) {
                                if (e != null) {
                                    T.showShort(getApplicationContext(), getString(R.string.verify_fail));
                                } else {
                                    if (list != null && list.size() > 0) {
                                        T.showShort(getApplicationContext(), getString(R.string.email_exist));
                                    } else {
                                        User user = new User();
                                        user.setEmail(email);
                                        //密码要经过md5加密
//                                        user.setPassword(MD5Encrypt.MD5Encode(pass));
                                        user.setPassword(pass);
                                        user.setUsername(name);
                                        user.signUp(new SaveListener<User>() {
                                            @Override
                                            public void done(User user, BmobException e) {
                                                if (e == null) {
                                                    T.showShort(getApplicationContext(), getString(R.string.regiter_succeed));
                                                    RegisterActivity.this.finish();



                                                } else {
                                                    T.showShort(getApplicationContext(), getString(R.string.username_exist));
                                                }

                                            }
                                        });


                                    }

                                }
                            }


                        });
                    }
                }).start();

            } else {
                T.showShort(this, getString(R.string.input_right_email));
            }
        } else {
            T.showShort(this, getString(R.string.input_full_info));
        }
    }

}
